package Practicas.practica4;


import data.IntegerDataSetGenerator;
import impl.MergeSorterBottonUp;
import impl.MergeSorterTopDown;
import test.BenchMark;

import java.util.ArrayList;
import java.util.Collections;

public class TestMerge {
        private ArrayList<Integer> list;

    private void testMiliTime(int valor, int sort) {
        list = new ArrayList<Integer>(valor);
        IntegerDataSetGenerator integerDataSetGenerator = new IntegerDataSetGenerator();

        MergeSorterBottonUp mergeSorterBottonUp = new MergeSorterBottonUp();
        MergeSorterTopDown mergeSorterTopDown = new MergeSorterTopDown();

        BenchMark benchMark = new BenchMark();
        mergeSorterBottonUp.addSorterListener(benchMark);
        mergeSorterTopDown.addSorterListener(benchMark);

        float random;
        random = 0;
        if (!list.isEmpty()) {
            list.clear();
        } else {
            list = (ArrayList<Integer>) integerDataSetGenerator.createDescending(valor);
            Collections.shuffle(list);
        }
        long tiempoRan1, tiempoRan2;
        int swapsRamdon, comparationsRandom;
        String nameSorter = "";
        tiempoRan1 = tiempoRan2 = swapsRamdon = comparationsRandom = 0;
        switch (sort) {
            case 0:
                tiempoRan1 = System.currentTimeMillis();
                mergeSorterBottonUp.sort(integerDataSetGenerator.getComparator(), list);
                tiempoRan2 = System.currentTimeMillis();
                random = tiempoRan2 - tiempoRan1;
                swapsRamdon = benchMark.getnSwap();
                comparationsRandom = benchMark.getComparations();
                nameSorter="burronUp";
                break;
            case 1:
                tiempoRan1 = System.currentTimeMillis();
                mergeSorterTopDown.sort(integerDataSetGenerator.getComparator(), list);
                tiempoRan2 = System.currentTimeMillis();
                random = tiempoRan2 - tiempoRan1;
                swapsRamdon = benchMark.getnSwap();
                comparationsRandom = benchMark.getComparations();
                nameSorter="TopDown";
                break;
        }
        System.out.println("||  Cant: " + valor + "  ||  Tiempo " + random + "  ||"+"nombre: "+nameSorter);
        System.out.println("||  Swaps: " + swapsRamdon + "  ||  Comparaciones " + comparationsRandom + "  ||");
        benchMark.resetAll();
    }

    public static void main(String[] args) {
        TestMerge tm = new TestMerge();
        int [] valores = {100,500,1000,2500,5000};
         for (int i = 0; i < valores.length; i++) {
                for (int s = 0; s < 2; s++) {
                    tm.testMiliTime(valores[i],s);
                }
                System.out.println("*******************************");
                System.out.println("*******************************");
                System.out.println("*******************************");
            }

    }

}
